package com.xlh.controller.statistic;

import com.github.pagehelper.PageInfo;
import com.xlh.aop.statistic.StatisticPreAcquireLock;
import com.xlh.common.Result;
import com.xlh.dto.PageDTO;
import com.xlh.dto.statistic.StatisticDTO;
import com.xlh.dto.statistic.StduentDTO;
import com.xlh.dto.user.UserInfoDTO;
import com.xlh.security.SpringSecurityUserInfoHolder;
import com.xlh.service.statistic.StatisticService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import static com.xlh.constant.StatisticConstant.STATISTIC;

/**
 * 统计
 * <p>
 * Created by lx on 2020/9/25.
 */
@Api(tags = "StudentController", description = "学生统计")
@RestController
@RequestMapping("/statistic/student")
public class StudentController {

    @Autowired
    private StatisticService statisticService;

    @ApiOperation("查询班级下的学生信息")
    @PostMapping("/class/{classId}/page")
    @PreAuthorize("@permissionValidator.isTeacher()")
//    @StatisticPreAcquireLock(value = STATISTIC, type = 2)
    public Result<PageDTO<UserInfoDTO>> listStudentStatisticPage(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @PathVariable("classId") Long classId, @RequestParam("courseId") Long courseId) {
        return Result.success(statisticService.listStudentByPage(pageNum, pageSize, classId, courseId));
    }

    @ApiOperation("班级学生统计下载")
    @PostMapping("/class/{classId}/download")
    @PreAuthorize("@permissionValidator.isTeacher()")
    @StatisticPreAcquireLock(value = STATISTIC, type = 2)
    public Result<PageInfo<UserInfoDTO>> downloadStudentStatistic(
            @PathVariable("classId") Long classId, @RequestParam("courseId") Long courseId) {
        statisticService.downloadStudentStatistic(classId, courseId);
        return Result.success();
    }

    @ApiOperation("查询班级下的学生信息")
    @GetMapping("/class/{classId}/user/{userId}")
    @PreAuthorize("@permissionValidator.isTeacher()")
//    @StatisticPreAcquireLock(value = STATISTIC, type = 2)
    public Result<StduentDTO> getStudentStatistic(
            @PathVariable("classId") Long classId, @PathVariable("userId") Long userId,
            @RequestParam("courseId") Long courseId) {
        return Result.success(statisticService.getStudentStatistic(classId, userId , courseId));
    }

    @ApiOperation("查询班级下的学生课程信息")
    @GetMapping("/class/{classId}/user/{userId}/course/{courseId}")
    @PreAuthorize("@permissionValidator.isTeacher()")
//    @StatisticPreAcquireLock(value = STATISTIC, type = 2)
    public Result<StatisticDTO> getStudentCourseStatistic(
            @PathVariable("courseId") Long courseId, @PathVariable("classId") Long classId,
            @PathVariable("userId") Long userId) {
        return Result.success(statisticService.getStudentCourseStatistic(courseId, classId, userId));
    }

    @ApiOperation("获取当前学生整体统计")
    @GetMapping
    @PreAuthorize("@permissionValidator.isStudent()")
//    @StatisticPreAcquireLock(value = STATISTIC, type = 2)
    public Result<StduentDTO> getStudentStatistic(@Autowired SpringSecurityUserInfoHolder userInfoHolder) {
        return Result.success(statisticService.getStudentStatistic(userInfoHolder.getUserId()));
    }

    @ApiOperation("获取当前学生某个课程统计")
    @GetMapping("/class/{classId}/user/course/{courseId}")
    @PreAuthorize("@permissionValidator.isStudent()")
//    @StatisticPreAcquireLock(value = STATISTIC, type = 2)
    public Result<StatisticDTO> getStudentDetailStatistic(
            @PathVariable("courseId") Long courseId, @PathVariable("classId") Long classId,
            @Autowired SpringSecurityUserInfoHolder userInfoHolder) {
        return Result.success(statisticService.getStudentCourseStatistic(
                courseId, classId, userInfoHolder.getUserId()));
    }

}
